package cn.tedu.ivosback.user.controller;

import cn.tedu.ivosback.base.response.JsonResult;
import cn.tedu.ivosback.user.pojo.dto.UserLoginParam;
import cn.tedu.ivosback.user.pojo.dto.UserQuery;
import cn.tedu.ivosback.user.pojo.dto.UserSaveParam;
import cn.tedu.ivosback.user.pojo.vo.UserVO;
import cn.tedu.ivosback.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    public JsonResult login(UserLoginParam userLoginParam) {
        log.debug("用户登录:userLoginParam={}", userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    public JsonResult save(UserSaveParam userSaveParam) {
        log.debug("用户保存:userVO={}", userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery) {
        log.debug("用户查询:userQuery={}", userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("重置密码，userId={}", userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId, @PathVariable String status) {
        log.debug("更新用户状态，userId={},status={}", userId, status);
        userService.updateStatus(userId, status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("删除用户，userId={}", userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAuditUser(@PathVariable Long parentId) {
        log.debug("查询审批人列表，parentId={}", parentId);
        List<UserVO> list = userService.selectAuditUser(parentId);
        return JsonResult.ok(list);
    }
}
